package com.valveinspectionsys.order.activity;

import java.util.List;

import com.valveinspectionsys.R;
import com.valveinspectionsys.dbhelp.XmlNameHelper;
import com.valveinspectionsys.gridview.ImageSwitcherActivity;
import com.valveinspectionsys.order.db.OrderDao;
import com.valveinspectionsys.povo.EngineeringOrderPoVo;
import com.valveinspectionsys.util.SharedPreferenceUtil;

import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.GestureDetector;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class OrderInfoActivity extends Activity implements OnClickListener {

    public Button button3;// 视频
    public Button button4;// 声音
    public Button button5;// 图片

    public TextView textview1;// 名称
    public TextView textview2;// 类型
    public TextView textview3;// 描述

    public GestureDetector mGestureDetector;
    public EngineeringOrderPoVo order = null;

    public int i = 0;// 当前页数

    private static final int dailogID = 1;

    private OrderDao dao;
    private SharedPreferenceUtil sp;
    private OrderQueryMainActivity act;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

	super.onCreate(savedInstanceState);
	setContentView(R.layout.orderinfo);
	act = (OrderQueryMainActivity) getParent();
	sp = new SharedPreferenceUtil(this);
	dao = new OrderDao(this, sp.getValue(XmlNameHelper.SqliteName));
	init();
    }

    /**
     * 初始化控件
     */
    public void init() {

	button3 = (Button) findViewById(R.id.orderinfo_video);
	button4 = (Button) findViewById(R.id.orderinfo_sound);
	button5 = (Button) findViewById(R.id.orderinfo_img);
	textview1 = (TextView) findViewById(R.id.orderinfo_name);
	textview2 = (TextView) findViewById(R.id.orderinfo_type);
	textview3 = (TextView) findViewById(R.id.orderinfo_desc);

	button3.setOnClickListener(this);
	button4.setOnClickListener(this);
	button5.setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {

	if (v.getId() == button3.getId()) {
	    if (order.getVideopath() != null && !order.getVideopath().equals("")) {

		Intent it = new Intent(Intent.ACTION_VIEW);
		it.setDataAndType(Uri.parse("file://" + order.getVideopath()), "video/avi");
		startActivity(it);

	    } else {
		Toast.makeText(OrderInfoActivity.this, "没有录视频", 1000).show();
	    }

	} else if (button4.getId() == v.getId()) {
	    if (order.getSoundpath() != null && !order.getSoundpath().equals("")) {
		    Intent it = new Intent(Intent.ACTION_VIEW);
		    it.setDataAndType(Uri.parse("file://" + order.getSoundpath()), "audio/amr");
		    startActivity(it);
	    } else {
		Toast.makeText(OrderInfoActivity.this, "没有录声音", 1000).show();

	    }

	} else if (button5.getId() == v.getId()) {
	    if (order == null ) {
		Toast.makeText(OrderInfoActivity.this, "没有工程单", Toast.LENGTH_LONG).show();
		return;
	    }

	    Intent intent = new Intent(OrderInfoActivity.this, ImageSwitcherActivity.class);
	    Bundle bundle = new Bundle();
	
	    intent.putExtra("status", "-1");
	    intent.putExtra("otherid", order.getId());
	    intent.putExtra("devicelistid", "-2");
	    intent.putExtras(bundle);

	    startActivity(intent);
	}
    }

    /**
     * 给界面添加数据
     */

    public void initData() {
	List<EngineeringOrderPoVo> list = dao.queryOrder(act.getOrderid());
	if (list == null || list.size() == 0) {
	    textview1.setText("");
	    textview2.setText("");
	    textview3.setText("");
	}

	if (list != null && list.size() > 0) {
	    order = list.get(i);
	    textview1.setText(order.getName());
	    textview2.setText(order.getTypenumber());
	    textview3.setText(order.getDesc());
	}

    }

    @Override
    protected void onDestroy() {
	dao.close();
	super.onDestroy();
    }

}
